package com.newgoldcurrency.activities.register;

import android.content.Intent;
import android.os.Bundle;
import android.os.SystemClock;

import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;

import com.newgoldcurrency.R;
import com.newgoldcurrency.databinding.ActivityRegisterPasswordBinding;
import com.newgoldcurrency.utils.Tools;

public class RegisterPasswordActivity extends AppCompatActivity {

    private ActivityRegisterPasswordBinding binding;
    private ActivityResultLauncher<Intent> launcher;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
         binding = ActivityRegisterPasswordBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
       setResult(1);
        binding.registerPasswordClose.setOnClickListener(v -> finish());
        binding.registerPasswordNext.setOnClickListener(v -> {
            if (checkDoubleClick()){
                String p1 = binding.registerPassword1.getText().toString();
                String p2 = binding.registerPassword2.getText().toString();

                if (p1.equals("")){
                    Tools.toast(getString(R.string.input_password));
                    return;
                }
                if (p2.equals("")){
                    Tools.toast(getString(R.string.input_password_again));
                    return;
                }
                if (!p1.equals(p2)){
                    Tools.toast(getString(R.string.password_atypism));
                    return;
                }
                if (Tools.passwordNotFormat(p1)){
                    Tools.toast(getString(R.string.password_format_err));
                    return;
                }

                Intent intent=new Intent(RegisterPasswordActivity.this,RegisterFriendActivity.class);
                intent.putExtra("password",p1);
                intent.putExtra("country",getIntent().getStringExtra("country"));
                intent.putExtra("email",getIntent().getStringExtra("email"));
                launcher.launch(intent);
            }

        });

        launcher=registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> {
            if (result.getResultCode()==2){
                setResult(2);
                finish();
            }
        });

    }
    private long mClickTime;
    private boolean checkDoubleClick() {
        boolean result= mClickTime < SystemClock.uptimeMillis() - 500;
        mClickTime = SystemClock.uptimeMillis();
        return result;
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        binding=null;
    }
}